﻿using JiveMessenger.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Windows.Input;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace JiveMessenger.CustomControls
{
    public sealed partial class MessageDownloadControl : UserControl
    {
//        public static readonly DependencyProperty DownloadMessagesCommandProperty =
//DependencyProperty.Register(
//"DownloadMessagesCommand",
//typeof(ICommand),
//typeof(MessageDownloadControl), new PropertyMetadata(null));

//        public ICommand DownloadMessagesCommand
//        {
//            get { return (ICommand)GetValue(DownloadMessagesCommandProperty); }
//            set { SetValue(DownloadMessagesCommandProperty, value); }
//        }

//        public static DependencyProperty DownloadMessagesCommandParameterProperty =
//DependencyProperty.Register("DownloadMessagesCommandParameter", typeof(object), typeof(MessageDownloadControl), new PropertyMetadata(null));
//        public object DownloadMessagesCommandParameter
//        {
//            get { return GetValue(DownloadMessagesCommandParameterProperty); }
//            set { SetValue(DownloadMessagesCommandParameterProperty, value); }
//        }



        public int DownloadCount
        {
            get { return (int)GetValue(DownloadCountProperty); }
            set { SetValue(DownloadCountProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DownloadCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DownloadCountProperty =
            DependencyProperty.Register("DownloadCount", typeof(int), typeof(MessageDownloadControl), new PropertyMetadata(50));



        public int DownloadAge
        {
            get { return (int)GetValue(DownloadAgeProperty); }
            set { SetValue(DownloadAgeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DownloadAge.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DownloadAgeProperty =
            DependencyProperty.Register("DownloadAge", typeof(int), typeof(MessageDownloadControl), new PropertyMetadata(24));




        public string ChatName
        {
            get { return (string)GetValue(ChatNameProperty); }
            set { SetValue(ChatNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ChatName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ChatNameProperty =
            DependencyProperty.Register("ChatName", typeof(string), typeof(MessageDownloadControl), new PropertyMetadata("this conversation"));



        public int AgeLimit
        {
            get { return (int)GetValue(AgeLimitProperty); }
            set { SetValue(AgeLimitProperty, value); }
        }

        // Using a DependencyProperty as the backing store for AgeLimit.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AgeLimitProperty =
            DependencyProperty.Register("AgeLimit", typeof(int), typeof(MessageDownloadControl), new PropertyMetadata(24, AgeLimitPropertyChanged));






        public int CountLimit
        {
            get { return (int)GetValue(CountLimitProperty); }
            set { SetValue(CountLimitProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CountLimit.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CountLimitProperty =
            DependencyProperty.Register("CountLimit", typeof(int), typeof(MessageDownloadControl), new PropertyMetadata(150));



        public MessageDownloadControl()
        {
            this.InitializeComponent();
            this.DataContext = this;

        }
        private static void AgeLimitPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }

        private void TextBoxDownloadCount_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            int count = -1;
            if (Int32.TryParse(tb.Text, out count))
            {
                if (count > CountLimit)
                    DownloadCount = CountLimit;
            }
        }

        private void TextBoxDownloadAge_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            int age = -1;
            if (Int32.TryParse(tb.Text, out age))
            {
                if (age > AgeLimit)
                    DownloadAge = AgeLimit;
            }
        }
        //private void bnDownloadMessages_Click(object sender, RoutedEventArgs e)
        //{
        //    this.DownloadMessagesCommandParameter = new MessageDownload(Convert.ToInt32(this.MessageCountSlider.Value), Convert.ToInt32(this.MessageAgeSlider.Value));
        //    if (this.DownloadMessagesCommand != null)
        //        this.DownloadMessagesCommand.Execute(this.DownloadMessagesCommandParameter);
        //}
    }
}
